//
//  HWVipliBaoViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/25.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWVipliBaoViewController.h"
#import "HWlibaozhongCollectionViewCell.h"
#import "HWlibaomoxingModel.h"


#import "HWGiftBaglibaoxiangqingViewController.h"

@interface HWVipliBaoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页

@end

@implementation HWVipliBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    
    
    [self setupUI];
    
    
    //添加控件
    [self addControlsview];
    
    
    [self addrefreshControl];
    
}



- (void)addrefreshControl {
    
    self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    [self.maincollectionView.mj_header beginRefreshing];
    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
    
}


//
- (void)underRefresh {
    DLog(@"精选");
    
    
    
    self.p  = @"1";
    
    self.dataMuArr = [[NSMutableArray alloc]init];
    
    
    
    
    
    NSString *typeStr = @"1";
    
    
    NSDictionary *parametes = @{
        @"ct" :  @"app",
        @"ac" : @"lb",
        @"p"  : self.p,
        @"ios" : @"1",
        @"type" : typeStr,
        @"member_id":[HWLocallyStoredData getCode],
        
        
    };
    
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        
        
        if ([codeStr intValue] == 1) {
            //  [MBProgressHUD  showError:@"加载成功"];
            NSArray *arr = response[@"data"];
            
            self.dataMuArr = [HWlibaomoxingModel mj_objectArrayWithKeyValuesArray:arr];
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            [self.maincollectionView reloadData];
            
            
            
            
            
            
            
        } else {
            
            self.dataMuArr = [[NSMutableArray alloc]init];
            
            [MBProgressHUD showError:response[@"message"]];
            
            
            
            [self.maincollectionView reloadData];
            
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        
        
    }];
    
    
    
    
    
    
    
}
//  上拉刷新

- (void)onRefresh {
    
    DLog(@"精选");
    
    if ([self.p intValue] > [self.max_p intValue]) {
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        
        return ;
        
    }
    
    
    
    NSString *typeStr = @"1";
    
    NSDictionary *parametes = @{
        @"ct" :  @"app",
        @"ac" : @"lb",
        @"type": typeStr,
        @"p"  : self.p,
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
    };
    
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        
        if ([codeStr intValue] == 1) {
            //  [MBProgressHUD  showError:@"加载成功"];
            NSArray *arr = response[@"data"];
            if (arr.count > 0) {
                
                
                NSMutableArray  *muArr  = [HWlibaomoxingModel mj_objectArrayWithKeyValuesArray:arr];
                
                for (int i = 0; i < muArr.count; i++) {
                    
                    [self.dataMuArr addObject:muArr[i]];
                    
                }
                
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                
                [self.maincollectionView reloadData];
                
                
            }
            
            
            
            
        } else {
            
            
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        
    }];
    
    
    
}








- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    
    CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
    //    layout.minimumInteritemSpacing = juanju-1;
    layout.minimumInteritemSpacing  = 1;
    
    
    //   layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-NavBarHeight-54-44) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
    // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
    // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
    //   [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
    [self.maincollectionView registerClass:[HWlibaozhongCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    //   [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    self.maincollectionView.userInteractionEnabled = YES;
    [self.view  addSubview:self.maincollectionView];
    
    [self.maincollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(kScreenW);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    
    
}



#pragma mark --------- UICollectionViewDelegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return self.dataMuArr.count;
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWlibaozhongCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    //   cell.backgroundColor =[UIColor yellowColor];
    //  HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
    //  cell.model = mode;
    
    
    HWlibaomoxingModel *model =       self.dataMuArr[indexPath.row];
    [cell.tupianicImageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:nil];
    cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",model.name];
    cell.libaoshulian.text = [NSString stringWithFormat:@"礼包数：%@",model.lb_num];
    
    
    
    if (indexPath.row%2== 0) {
        cell.backgroundColor = [UIColor whiteColor];
        
        
    } else {
        
        cell.backgroundColor = [UIColor whiteColor];
        
        
        
    }
    
    
    
    
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((kScreenW-2)/2,62);
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
    
    HWlibaomoxingModel *model =   self.dataMuArr[indexPath.row];
    HWGiftBaglibaoxiangqingViewController *gameDetails = [[HWGiftBaglibaoxiangqingViewController alloc]init];
    
    gameDetails.gidStr = model.gid;
    gameDetails.vip = @"1";
    [self.navigationController pushViewController:gameDetails animated:YES];
    
    
}






@end
